package com.example.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.HttpStatusEnum;
import com.example.entity.ResponseData;
import com.example.entity.TreeMenuNav;
import com.example.entity.TreeOptions;
import com.example.entity.TreeRuleMenuChecks;
import com.example.mapper.MenuMapper;
import com.example.pojo.RuleMenu;
import com.example.mapper.RuleMenuMapper;
import com.sun.source.tree.Tree;
import org.springframework.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 角色菜单关联表(RuleMenu)表服务实现类
 *
 * @email: pengyujun53@163.com
 * @author: peng_YuJun
 * @date: 2024/01/17
 * @time: 23:13
 */
@Service
public class RuleMenuService {
    /**
     * mapper对象
     */
    @Resource
    private RuleMenuMapper ruleMenuMapper;
    @Resource
    private MenuMapper menuMapper;

    /**
     * 分页查询
     *
     * @param currentPage
     * @param pageSize
     * @param ruleMenu    筛选条件
     * @return
     */
    public ResponseData getByPage(Integer currentPage, Integer pageSize, RuleMenu ruleMenu) {
        QueryWrapper<RuleMenu> qw = new QueryWrapper<>();
        //TODO: getByPage ruleMenu qw查询条件待构建
        IPage ipage = new Page(currentPage, pageSize);
        ruleMenuMapper.selectPage(ipage, qw);
        //若当前页码大于总页面数
        if (currentPage > ipage.getPages()) {
            ipage = new Page(ipage.getPages(), pageSize);
            ruleMenuMapper.selectPage(ipage, qw);
        }
        if (ipage.getTotal() <= 0) {
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(), ipage);
    }

    /**
     * 根据ID查询信息
     *
     * @param id 主键
     * @return 单条数据
     */
    public ResponseData findById(Long id) {
        RuleMenu ruleMenu = ruleMenuMapper.selectById(id);
        if (ruleMenu == null || ruleMenu.getId() == null) {
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        //返回结果
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(), ruleMenu);
    }

    /**
     * 新增数据
     *
     * @param ruleMenu 实体
     * @return 新增结果
     */
    public ResponseData add(RuleMenu ruleMenu) {
        //插入
        if (ruleMenuMapper.insert(ruleMenu) > 0) {
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "新增失败，请稍后再试");
    }

    /**
     * 更新数据
     *
     * @param ruleMenu 实体
     * @return 更新结果
     */
    public ResponseData update(RuleMenu ruleMenu) {
        //更新
        if (ruleMenuMapper.updateById(ruleMenu) > 0) {
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "修改失败，请稍后再试");
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return
     */
    public ResponseData delete(Long id) {
        if (ruleMenuMapper.deleteById(id) > 0) {
            //返回成功响应
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "删除失败，请稍后再试");
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    public ResponseData deleteBatch(List<Long> idList) {
        if (ruleMenuMapper.deleteBatchIds(idList) > 0) {
            //返回成功响应
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "删除失败，请稍后再试");
    }

    /**
     * 树形多选列表查询
     *
     * @param ruleCode
     * @return
     */
    public ResponseData getTreeChecks(String ruleCode, String parent) {
        // 查询已关联的菜单code
        LambdaQueryWrapper<RuleMenu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.hasText(ruleCode), RuleMenu::getRuleCode, ruleCode);
        lqw.select(RuleMenu::getMenuCode);
        List<Object> checkMenuCodeList = ruleMenuMapper.selectObjs(lqw);
        //返回结果
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(), checkMenuCodeList);
    }

    /**
     * 关联更改
     *
     * @param ruleCode
     * @param menuCodeList
     * @return
     */
    public ResponseData alterRelate(String ruleCode, List<String> menuCodeList) {
        // 先删除
        LambdaQueryWrapper<RuleMenu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.hasText(ruleCode), RuleMenu::getRuleCode, ruleCode);
        ruleMenuMapper.delete(lqw);
        // 再插入
        if (menuCodeList != null && !menuCodeList.isEmpty()) {
            List<RuleMenu> ruleMenus = menuCodeList.stream()
                    .map(menuCode -> {
                        RuleMenu ruleMenu = new RuleMenu();
                        ruleMenu.setMenuCode(menuCode);
                        ruleMenu.setRuleCode(ruleCode);
                        return ruleMenu;
                    })
                    .collect(Collectors.toList());
            ruleMenuMapper.insertBatch(ruleMenus);
        }
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
    }

    /**
     * 获取用户菜单
     * @param parent
     * @param ruleCode
     * @return
     */
    public ResponseData getUserMenu(String ruleCode, String parent) {
        // 查询
        List<TreeMenuNav> treeMenuNavs = ruleMenuMapper.selectTreeMenuNav(ruleCode, parent);
        if (treeMenuNavs == null || treeMenuNavs.isEmpty()) {
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        //返回结果
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(), treeMenuNavs);
    }
}
